package com.cl.controller;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.util.*;
import jakarta.servlet.http.HttpServletRequest;

import com.cl.utils.ValidatorUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.cl.annotation.IgnoreAuth;
import com.cl.annotation.SysLog;

import com.cl.entity.OrdersEntity;
import com.cl.entity.view.OrdersView;

import com.cl.service.OrdersService;
import com.cl.service.TokenService;
import com.cl.utils.PageUtils;
import com.cl.utils.R;
import com.cl.utils.EncryptUtil;
import com.cl.utils.MPUtil;
import com.cl.utils.MapUtils;
import com.cl.utils.CommonUtil;
import java.io.IOException;

/**
 * 商品订单
 * 后端接口
 * @author 
 * @email 
 * @date 2025-03-19 16:07:08
 */
@RestController
@RequestMapping("/orders")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;







    /**
     * 后台列表
     */
    @RequestMapping("/page")
    public R page(@RequestParam Map<String, Object> params,OrdersEntity orders,
                                                                                                                                                                                                                                                                                                                    HttpServletRequest request){
                    String tableName = request.getSession().getAttribute("tableName").toString();
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    if(tableName.equals("merchant")) {
                    orders.setMerchantAccount((String)request.getSession().getAttribute("username"));
                                            if(orders.getUserid()!=null) {
                            orders.setUserid(null);
                        }
                                    }
                                        else {
                        if(!request.getSession().getAttribute("role").toString().equals("管理员")) {
                            orders.setUserid((Long)request.getSession().getAttribute("userId"));
                        }
                    }
                                                                QueryWrapper<OrdersEntity> ew = new QueryWrapper<OrdersEntity>();
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
        
        
        PageUtils page = ordersService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, orders), params), params));
        return R.ok().put("data", page);
    }







    /**
     * 前端列表
     */
	@IgnoreAuth
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params,OrdersEntity orders,
		HttpServletRequest request){
        QueryWrapper<OrdersEntity> ew = new QueryWrapper<OrdersEntity>();

		PageUtils page = ordersService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, orders), params), params));
        return R.ok().put("data", page);
    }

	/**
     * 列表
     */
    @RequestMapping("/lists")
    public R list( OrdersEntity orders){
       	QueryWrapper<OrdersEntity> ew = new QueryWrapper<OrdersEntity>();
      	ew.allEq(MPUtil.allEQMapPre( orders, "orders"));
        return R.ok().put("data", ordersService.selectListView(ew));
    }

	 /**
     * 查询
     */
    @RequestMapping("/query")
    public R query(OrdersEntity orders){
        QueryWrapper< OrdersEntity> ew = new QueryWrapper< OrdersEntity>();
 		ew.allEq(MPUtil.allEQMapPre( orders, "orders"));
		OrdersView ordersView =  ordersService.selectView(ew);
		return R.ok("查询商品订单成功").put("data", ordersView);
    }

    /**
     * 后端详情
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
        OrdersEntity orders = ordersService.getById(id);
		orders = ordersService.selectView(new QueryWrapper<OrdersEntity>().eq("id", id));
        return R.ok().put("data", orders);
    }

    /**
     * 前端详情
     */
	@IgnoreAuth
    @RequestMapping("/detail/{id}")
    public R detail(@PathVariable("id") Long id){
        OrdersEntity orders = ordersService.getById(id);
		orders = ordersService.selectView(new QueryWrapper<OrdersEntity>().eq("id", id));
        return R.ok().put("data", orders);
    }




    /**
     * 后端保存
     */
    @RequestMapping("/save")
    @SysLog("新增商品订单")
    public R save(@RequestBody OrdersEntity orders, HttpServletRequest request){
        //ValidatorUtils.validateEntity(orders);
    	orders.setUserid((Long)request.getSession().getAttribute("userId"));
        ordersService.save(orders);
        return R.ok().put("data",orders.getId());
    }

    /**
     * 前端保存
     */
    @SysLog("新增商品订单")
    @RequestMapping("/add")
    public R add(@RequestBody OrdersEntity orders, HttpServletRequest request){
    	//ValidatorUtils.validateEntity(orders);
        ordersService.save(orders);
        return R.ok();
    }



    /**
     * 修改
     */
    @RequestMapping("/update")
    @Transactional
    @SysLog("修改商品订单")
    public R update(@RequestBody OrdersEntity orders, HttpServletRequest request){
        //ValidatorUtils.validateEntity(orders);
        ordersService.updateById(orders);//全部更新
        return R.ok();
    }

    /**
     * 审核
     */
    @RequestMapping("/shBatch")
    @Transactional
    @SysLog("审核商品订单")
    public R update(@RequestBody Long[] ids, @RequestParam String sfsh, @RequestParam String shhf){
        List<OrdersEntity> list = new ArrayList<OrdersEntity>();
        for(Long id : ids) {
            OrdersEntity orders = ordersService.getById(id);
            orders.setSfsh(sfsh);
            orders.setShhf(shhf);
            list.add(orders);
        }
        ordersService.updateBatchById(list);
        return R.ok();
    }




    /**
     * 删除
     */
    @RequestMapping("/delete")
    @SysLog("删除商品订单")
    public R delete(@RequestBody Long[] ids){
        ordersService.removeBatchByIds(Arrays.asList(ids));
        return R.ok();
    }











}
